<?php
require('header.php');
$msgError = '';
if(isset($_POST['username'])){
	$username = cleanValue($_POST['username']);
	$password = $_POST['password'];
	$email = cleanValue($_POST['email']);
	$msgError = '';
	if(strlen(trim($username)) < 5){
		$msgError .= '<p>User name must be 5 characters or more</p>';
	}
	if(md5($password) != md5($_POST['repassword'])){
		$msgError .= '<p>Password does not match</p>';
	}
	if(isUserExist($username, $email)){
		$msgError .= '<p>Username or email already in used.</p>';
	}
	if($msgError == ''){
		$pwd = generateSaltedPassword($password);
		if(addUser(array(
			'user_name' => $username,
			'user_password' => $pwd,
			'user_email' => $email,
			'user_activate' => 'YES'
		))){
			echo '<div class="alert alert-success" style="text-align: center;">Register successful <a href="'.route('login').'"><button type="button" class="btn btn-primary">Login now <span class="glyphicon glyphicon-log-in"></button></a>.</div>';
		}
		else{
			$msgError .= '<p>Error when adding new user:<br />'.mysql_error().'</p>';
		}
	}
}
?>
<script type="text/javascript">
$(document).ready(function(){
	$('#btn-save').click(function(){
		var isError = false;
		if($('#username').val().length < 5){
			$('.fg-username').addClass('has-error');
			isError = true;
		}
		if($('#password').val() == '' || $('#password').val() != $('#repassword').val()){
			$('.fg-password').addClass('has-error');
			isError = true;
		}
		if($('#email').val().length < 6){
			$('.fg-email').addClass('has-error');
			isError = true;
		}
		if(!isError){
			return true;
		}

		return false;
	});
});
</script>
<div class="well">
	<?php if($msgError != ''): ?>
	<div class="alert alert-danger">
		<?php echo $msgError; ?>
	</div>
	<?php endif; ?>

	<form class="form-horizontal" role="form" method="post">
		<div class="form-group fg-username">
		    <label for="username" class="col-sm-4 control-label">Username</label>
		    <div class="col-sm-4">
		      <input type="text" name="username" class="form-control" id="username" value="<?php echo $uname = isset($_POST['username']) ? cleanValue($_POST['username']) : '' ?>">
		    </div>
		</div>
		<div class="form-group fg-password">
		    <label for="password" class="col-sm-4 control-label">Password</label>
		    <div class="col-sm-4">
		      <input type="password" name="password" class="form-control" id="password" value="">
		    </div>
		</div>
		<div class="form-group fg-password">
		    <label for="repassword" class="col-sm-4 control-label">Re-type password</label>
		    <div class="col-sm-4">
		      <input type="password" name="repassword" class="form-control" id="repassword" value="">
		    </div>
		</div>
		<div class="form-group fg-email">
		    <label for="email" class="col-sm-4 control-label">Email</label>
		    <div class="col-sm-4">
		      <input type="text" name="email" class="form-control" id="email" value="<?php echo $uemail = isset($_POST['email']) ? cleanValue($_POST['email']) : '' ?>" placeholder="example@domain.com">
		    </div>
		</div>

		<div class="form-group">
			<div class="col-sm-offset-4 col-sm-8">
			  <button type="submit" id="btn-save" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk"></span> Register</button>
			</div>
		</div>
	</form>
</div>
<?php require('footer.php'); ?>